'use strict';

const qs = require('qs');
const mockjs = require('mockjs');

var Log = require('log')
  , log = new Log('info');

// 用户模拟数据
let tableListData = {};
if (!global.tableListData) {
  const data = mockjs.mock({
    'data|100': [{
      'id|+1': 1,
      user_id: '5672284260b2260ee42d7c07',
      user_name: '@cname',
      'balance|0-990000': 1,
      'tip_balance|0-990000': 1,
      'total_balance|0-990000': 1,
      is_enabled: '1',
    }],
    page: {
      total: 100,
      current: 1
    }
  });
  tableListData = data;
  global.tableListData = tableListData;
} else {
  tableListData = global.tableListData;
}

// 充值历史纪录模拟数据
let tableListRData = {};
if (!global.tableListRData) {
  const data = mockjs.mock({
    'data|100': [{
      'id|+1': 1,
      trade_no: 'METX1476426758236810903',
      trade_name: '充值',
      order_id: 'feb8b25a-91d7-11e6-b30a-00163e0c0574',
      user_id: '5672284260b2260ee42d7c07',
      //'user_id|+1': 1,
      nick_name: '@cname',
     'amount|+1': 100910,
      time: '2011-12-12 12:12:12',
      complete_time: '2011-12-12 12:12:12',
      pay_status: 1,
      platform: 1
    }],
    page: {
      total: 100,
      current: 1
    }
  });
  tableListRData = data;
  global.tableListRData = tableListRData;
} else {
  tableListRData = global.tableListRData;
}

// 充值历史纪录模拟数据
let tableListUHData = {};
if (!global.tableListUHData) {
  const data = mockjs.mock({
    'data|6': [{
      'id|+1': 1,
      user_id: '5672284260b2260ee42d7c07',

      user_name: '@cname',
      account_type: 1,
      currency: 1,
      trade_id: 'feb8b25a-91d7-11e6-b30a-00163e0c0574',
      'trade_money|0-990000': 1,
      trade_type: 1,
      'balance|0-990000': 1,
      create_time: '2011-12-12 12:12:12',
    }],
    page: {
      total: 6,
      current: 1
    }
  });
  tableListUHData = data;
  global.tableListUHData = tableListUHData;
} else {
  tableListUHData = global.tableListUHData;
}

// 充值历史纪录模拟数据
let tableListWData = {};
if (!global.tableListWData) {
  const data = mockjs.mock({
    'data|100': [{
      'id|+1': 1,
      trade_no: 'METX1476426758236810903',
      trade_name: '提现申请',
      user_id: '5672284260b2260ee42d7c07',
      nick_name: '@cname',
      'total_money|+1': 100910,
      'money|+1': 100910,
      'profit|+1': 100910,
      rate: 75,
      time: '2011-12-12 12:12:12',
      complete_time: '2011-12-12 12:12:12',
      pay_status: 1,
      memo: "准予提现。"
    }],
    page: {
      total: 100,
      current: 1
    }
  });
  tableListWData = data;
  global.tableListWData = tableListWData;
} else {
  tableListWData = global.tableListWData;
}


module.exports = {

  ////////////////////////////////////////////////////////////////
///////////////////////////////充值纪录//////////////////////////
////////////////////////////////////////////////////////////////

  'GET /api/recharge' (req, res) {
    const page = qs.parse(req.query);
    const pageSize = page.pageSize || 10;
    const currentPage = page.page || 1;

    let data;
    let newPage;

    let newData = tableListRData.data.concat();

    if (page.field) {
      const d = newData.filter(function (item) {
        return item[page.field].indexOf(page.keyword) > -1;
      });

      data = d.slice((currentPage - 1) * pageSize, currentPage * pageSize);

      newPage = {
        current: currentPage * 1,
        total: d.length
      };
    } else {
      data = tableListRData.data.slice((currentPage - 1) * pageSize, currentPage * pageSize);
      tableListRData.page.current = currentPage * 1;
      newPage = {
        current: tableListRData.page.current,
        total: tableListRData.page.total
      };
    }


    setTimeout(function () {
      res.json({
        success: true,
        data,
        page: newPage
      });
    }, 500);
  },

  'POST /api/recharge' (req, res) {
    setTimeout(function () {
      const newData = qs.parse(req.body);

      newData.id = tableListRData.data.length + 1;
      tableListRData.data.unshift(newData);

      tableListRData.page.total = tableListRData.data.length;
      tableListRData.page.current = 1;

      global.tableListRData = tableListRData;
      res.json({
        success: true,
        data: tableListRData.data,
        page: tableListRData.page
      });
    }, 500);
  },

  'DELETE /api/recharge' (req, res) {
    setTimeout(function () {
      const deleteItem = qs.parse(req.body);

      tableListRData.data = tableListRData.data.filter(function (item) {
        if (item.id == deleteItem.id) {
          return false;
        }
        return true;
      });

      tableListRData.page.total = tableListRData.data.length;

      global.tableListRData = tableListRData;
      res.json({
        success: true,
        data: tableListRData.data,
        page: tableListRData.page
      });
    }, 500);
  },

////////////////////////////////////////////////////////////////
///////////////////////////////用户纪录//////////////////////////
////////////////////////////////////////////////////////////////

   'GET /api/users' (req, res) {
    const page = qs.parse(req.query);
    const pageSize = page.pageSize || 10;
    const currentPage = page.page || 1;

    let data;
    let newPage;

    let newData = tableListData.data.concat();

    if (page.field) {
      const d = newData.filter(function (item) {
        return item[page.field].indexOf(page.keyword) > -1;
      });

      data = d.slice((currentPage - 1) * pageSize, currentPage * pageSize);

      newPage = {
        current: currentPage * 1,
        total: d.length
      };
    } else {
      data = tableListData.data.slice((currentPage - 1) * pageSize, currentPage * pageSize);
      tableListData.page.current = currentPage * 1;
      newPage = {
        current: tableListData.page.current,
        total: tableListData.page.total
      };
    }


    setTimeout(function () {
      res.json({
        success: true,
        data,
        page: newPage
      });
    }, 500);
  },

  'POST /api/users' (req, res) {
    setTimeout(function () {
      const newData = qs.parse(req.body);

      newData.id = tableListData.data.length + 1;
      tableListData.data.unshift(newData);

      tableListData.page.total = tableListData.data.length;
      tableListData.page.current = 1;

      global.tableListData = tableListData;
      res.json({
        success: true,
        data: tableListData.data,
        page: tableListData.page
      });
    }, 500);
  },

  'DELETE /api/users' (req, res) {
    setTimeout(function () {
      const deleteItem = qs.parse(req.body);

      tableListData.data = tableListData.data.filter(function (item) {
        if (item.id == deleteItem.id) {
          return false;
        }
        return true;
      });

      tableListData.page.total = tableListData.data.length;

      global.tableListData = tableListData;
      res.json({
        success: true,
        data: tableListData.data,
        page: tableListData.page
      });
    }, 500);
  },

  ////////////////////////////////////////////////////////////////
///////////////////////////////用户历史纪录//////////////////////
////////////////////////////////////////////////////////////////

   'GET /api/userhistory' (req, res) {
    const page = qs.parse(req.query);
    const pageSize = page.pageSize || 10;
    const currentPage = page.page || 1;

    let data;
    let newPage;

    let newData = tableListUHData.data.concat();

    if (page.field) {
      log.info(page.field);
      const d = newData.filter(function (item) {
        return item[page.field].indexOf(page.keyword) > -1;
      });

      data = d.slice((currentPage - 1) * pageSize, currentPage * pageSize);

      newPage = {
        current: currentPage * 1,
        total: d.length
      };
    } else {
      data = tableListUHData.data.slice((currentPage - 1) * pageSize, currentPage * pageSize);
      tableListUHData.page.current = currentPage * 1;
      newPage = {
        current: tableListUHData.page.current,
        total: tableListUHData.page.total
      };
    }


    setTimeout(function () {
      res.json({
        success: true,
        data,
        page: newPage
      });
    }, 500);
  },

////////////////////////////////////////////////////////////////
///////////////////////////////提现纪录//////////////////////
////////////////////////////////////////////////////////////////
'GET /api/withdraws' (req, res) {
    const page = qs.parse(req.query);
    const pageSize = page.pageSize || 10;
    const currentPage = page.page || 1;

    let data;
    let newPage;

    let newData = tableListWData.data.concat();

    if (page.field) {
      log.info(page.field);
      const d = newData.filter(function (item) {
        return item[page.field].indexOf(page.keyword) > -1;
      });

      data = d.slice((currentPage - 1) * pageSize, currentPage * pageSize);

      newPage = {
        current: currentPage * 1,
        total: d.length
      };
    } else {
      data = tableListWData.data.slice((currentPage - 1) * pageSize, currentPage * pageSize);
      tableListWData.page.current = currentPage * 1;
      newPage = {
        current: tableListWData.page.current,
        total: tableListWData.page.total
      };
    }


    setTimeout(function () {
      res.json({
        success: true,
        data,
        page: newPage
      });
    }, 500);
  },





};
